from manim import *

from manim import *

class IntegerAdditionWithSquares2(Scene):
    def construct(self):
        # 创建数轴
        number_line = NumberLine(
            x_range=[0, 10, 1],  # 数轴范围从 0 到 10，每个刻度为 1
            length=10,           # 数轴的长度
            color=BLUE,
            include_numbers=True,  # 显示刻度数字
        )
        number_line.shift(DOWN * 2)  # 将数轴向下移动
        self.play(Create(number_line))

        # 创建 y 轴
        y_axis = NumberLine(
            x_range=[-3, 4, 1],  # y 轴范围从 -3 到 4，每个刻度为 1
            length=7,            # y 轴的长度（包括负数部分）
            color=BLUE,
            include_numbers=True,  # 显示刻度数字
            rotation=PI / 2      # 旋转90度变为y轴
        )
        y_axis.shift(LEFT * 5+DOWN * 0.5)  # 将 y 轴向左移动到 x 轴的起点
        self.play(Create(y_axis))

        # 定义整数加法
        a = 2
        b = 3
        sum_ab = a + b

        # 创建整数 a 和 b 的单位正方形
        squares_a = self.create_unit_squares(a, color=GREEN)
        squares_b = self.create_unit_squares(b, color=ORANGE)

        # 将整数 a 的正方形移动到数轴上
        for square in squares_a:
            self.play(square.animate.move_to(number_line.n2p(square.get_center()[0])+RIGHT*0.5 + UP * 0.5))

        # 将整数 b 的正方形移动到数轴上
        for square in squares_b:
            self.play(square.animate.move_to(number_line.n2p(square.get_center()[0] + a)+RIGHT*0.5 + UP * 0.5))

        # 显示最终的和 sum_ab 的正方形（只是为了强调结果）
        squares_sum = self.create_unit_squares(sum_ab, color=YELLOW, opacity=0.2)

        # 将整数 sum_ab 的正方形移动到数轴上
        for square in squares_sum:
            self.play(square.animate.move_to(number_line.n2p(square.get_center()[0])+RIGHT*0.5 + UP * 0.5))

        # 添加加法的文本说明
        explanation = Text(f"{a} + {b} = {sum_ab}", font_size=36).to_edge(DOWN)
        self.play(Write(explanation))

        # 保持屏幕显示几秒钟
        self.wait(3)

    def create_unit_squares(self, n, color=BLUE, opacity=1.0):
        # 创建 n 个单位正方形
        squares = VGroup()
        for i in range(n):
            square = Square(side_length=1, fill_color=color, fill_opacity=opacity)
            square.move_to(RIGHT * i)  # 设置初始位置
            squares.add(square)
        return squares


class IntegerAdditionWithSquares1(Scene):
    def construct(self):
        # 创建数轴
        number_line = NumberLine(
            x_range=[0, 10, 1],  # 数轴范围从 0 到 10，每个刻度为 1
            length=10,           # 数轴的长度
            color=BLUE,
            include_numbers=True,  # 显示刻度数字
        )
        number_line.shift(DOWN * 2)  # 将数轴向下移动
        self.play(Create(number_line))

        # 创建 y 轴
        y_axis = NumberLine(
            x_range=[-3, 4, 1],  # y 轴范围从 -3 到 4，每个刻度为 1
            length=7,            # y 轴的长度（包括负数部分）
            color=BLUE,
            include_numbers=True,  # 显示刻度数字
            rotation=PI / 2      # 旋转90度变为y轴
        )
        y_axis.shift(LEFT * 5+DOWN * 0.5)  # 将 y 轴向左移动到 x 轴的起点
        self.play(Create(y_axis))

        # 定义整数加法
        a = 3
        b = 4
        sum_ab = a + b

        # 显示起始整数 a 的正方形
        self.create_unit_squares(a, number_line, start_pos=0, color=GREEN)

        # 显示整数 b 的正方形，放在整数 a 之后
        self.create_unit_squares(b, number_line, start_pos=a, color=ORANGE)

        # 显示最终的和 sum_ab 的正方形（只是为了强调结果）
        self.create_unit_squares(sum_ab, number_line, start_pos=0, color=YELLOW, opacity=0.2)

        # 添加加法的文本说明
        explanation = Text(f"{a} + {b} = {sum_ab}", font_size=36).to_edge(DOWN)
        self.play(Write(explanation))

        # 保持屏幕显示几秒钟
        self.wait(3)

    def create_unit_squares(self, n, number_line, start_pos=0, color=BLUE, opacity=1.0):
        # 在数轴上创建 n 个单位正方形，底边落在 x 轴上
        squares = VGroup()
        for i in range(n):
            square = Square(side_length=1, fill_color=color, fill_opacity=opacity)
            square.move_to(number_line.n2p(start_pos + i + 0.5) + UP * 0.5)  # 移动正方形到底边落在x轴上
            squares.add(square)
        self.play(Create(squares))



class IntegerAdditionAndMultiplication(Scene):
    def construct(self):
        # 创建数轴
        number_line = NumberLine(
            x_range=[0, 10, 1],  # 数轴范围从 0 到 10，每个刻度为 1
            length=10,           # 数轴的长度
            color=BLUE,
            include_numbers=True,  # 显示刻度数字
        )
        number_line.shift(DOWN * 2)  # 将数轴向下移动
        self.play(Create(number_line))

        # 创建 y 轴
        y_axis = NumberLine(
            x_range=[0, 10, 1],  # y 轴范围从 0 到 10，每个刻度为 1
            length=5,            # y 轴的长度
            color=BLUE,
            include_numbers=True,  # 显示刻度数字
            rotation=PI / 2      # 旋转90度变为y轴
        )
        y_axis.shift(LEFT * 5)  # 将 y 轴向左移动到 x 轴的起点
        self.play(Create(y_axis))

        # 定义整数加法和乘法
        a = 2
        b = 3
        sum_ab = a + b
        product_ab = a * b

        # 显示整数 a 和 b 的单位正方形
        self.create_unit_squares(a, number_line, start_pos=0, color=GREEN)
        self.create_unit_squares(b, number_line, start_pos=a, color=ORANGE)

        # 显示加法和乘法的结果正方形
        self.create_unit_squares(sum_ab, number_line, start_pos=0, color=YELLOW, opacity=0.2)

        # 添加加法的文本说明
        addition_explanation = Text(f"{a} + {b} = {sum_ab}", font_size=36).to_edge(UP)
        self.play(Write(addition_explanation))

        # 显示乘法的正方形区域
        self.create_multiplication_area(a, b, number_line, start_pos=0, color=PURPLE, opacity=0.5)

        # 添加乘法的文本说明
        multiplication_explanation = Text(f"{a} * {b} = {product_ab}", font_size=36).next_to(addition_explanation, DOWN)
        self.play(Write(multiplication_explanation))

        # 保持屏幕显示几秒钟
        self.wait(3)

    def create_unit_squares(self, n, number_line, start_pos=0, color=BLUE, opacity=1.0):
        # 在数轴上方创建 n 个单位正方形
        squares = VGroup()
        for i in range(n):
            square = Square(side_length=1, fill_color=color, fill_opacity=opacity)
            square.move_to(number_line.n2p(start_pos + i + 0.5) + UP)  # 移动正方形到对应的位置
            squares.add(square)
        self.play(Create(squares))

    def create_multiplication_area(self, a, b, number_line, start_pos=0, color=PURPLE, opacity=0.5):
        # 在数轴上方创建 a*b 的矩形区域
        rect = Rectangle(width=a, height=b, fill_color=color, fill_opacity=opacity)
        rect.move_to(number_line.n2p(start_pos + a / 2) + UP * (b / 2))  # 移动矩形到对应的位置
        self.play(Create(rect))


class IntegerAdditionWithSquares(Scene):
    def construct(self):
        # 创建数轴
        number_line = NumberLine(
            x_range=[0, 10, 1],  # 数轴范围从 0 到 10，每个刻度为 1
            length=10,           # 数轴的长度
            color=BLUE,
            include_numbers=True,  # 显示刻度数字
        )
        self.play(Create(number_line))

        # 定义整数加法
        a = 2
        b = 3
        sum_ab = a + b

        # 显示起始整数 a 的正方形
        self.create_unit_squares(a, number_line, color=GREEN)

        # 显示整数 b 的正方形，放在整数 a 之后
        self.create_unit_squares(b, number_line, start_pos=a, color=ORANGE)

        # 显示最终的和 sum_ab 的正方形（只是为了强调结果）
        self.create_unit_squares(sum_ab, number_line, start_pos=0, color=YELLOW, opacity=0.2)

        # 添加文本说明
        explanation = Text(f"{a} + {b} = {sum_ab}", font_size=36).to_edge(DOWN)
        self.play(Write(explanation))

        # 保持屏幕显示几秒钟
        self.wait(3)

    def create_unit_squares(self, n, number_line, start_pos=0, color=BLUE, opacity=1.0):
        # 在数轴上创建 n 个单位正方形
        squares = VGroup()
        for i in range(n):
            square = Square(side_length=1, fill_color=color, fill_opacity=opacity)
            square.move_to(number_line.n2p(start_pos + i + 0.5))  # 移动正方形到对应的位置
            squares.add(square)
        self.play(Create(squares))

# 执行渲染
if __name__ == "__main__":
    scene = IntegerAdditionWithSquares()
    scene.render()


class IntegerAddition(Scene):
    def construct(self):
        # 创建数轴
        number_line = NumberLine(
            x_range=[-10, 10, 1],  # 数轴范围从 -10 到 10，每个刻度为 1
            length=10,             # 数轴的长度
            color=BLUE,
            include_numbers=True,  # 显示刻度数字
        )
        self.play(Create(number_line))

        # 创建并显示整数点
        start_value = 2
        start_point = Dot(point=number_line.n2p(start_value), color=YELLOW)  # 起始点位置为 2
        self.play(FadeIn(start_point))
        start_label = MathTex(str(start_value)).next_to(start_point, UP)
        self.play(Write(start_label))

        # 加法运算，移动到新的位置
        add_value = 3  # 需要加的整数
        end_value = start_value + add_value
        self.play(start_point.animate.move_to(number_line.n2p(end_value)), run_time=2)  # 移动到加法结果位置
        end_label = MathTex(f"{start_value} + {add_value} = {end_value}").next_to(start_point, UP)
        self.play(Transform(start_label, end_label))

        # 显示加法过程中的箭头
        arrow = Arrow(start=number_line.n2p(start_value), end=number_line.n2p(end_value), buff=0, color=RED)
        self.play(Create(arrow))

        # 加上文本说明
        explanation = Text(f"{start_value} + {add_value} = {end_value}", font_size=24).to_edge(DOWN)
        self.play(Write(explanation))

        # 保持屏幕显示几秒钟
        self.wait(3)
